<?php
    if (!defined('APPPATH')) exit('No direct script access allowed');

    require_once(APPPATH.'libraries/utils/translation/class.translation.php');
    
    if(isset($_GET['lang']))
        $translate = new Translation($_GET['lang'], false);
    else {
        if(!empty($lang))
            $translate = new Translation($lang, false);
        else
            $translate = new Translation('de', false);
    }
?>
<?php
    function submenusTree($menus, $depth) {
        $space = "";
        for($i = 0; $i < $depth; $i++)
            $space .= "&nbsp;&nbsp;&nbsp;";
        
        foreach ($menus as $key => $menu) {
            echo "<tr>";
            echo "    <td>" . $space . "<img src=" . IMAGEPATH . "leaf.png width=\"20\" height=\"20\" alt=\"agent24\" />" . $menu['name'] . "</td>";
            echo "    <td><a href=" . site_url() . "/admin/menu_controller/order/0/" . $menu['menu_id'] . "><img src=" . IMAGEPATH . "arrow_up.png width=\"15px\" height=\"15px\" alt=\"agent24\" /></a> | <a href=" . site_url() . "/admin/menu_controller/order/1/" . $menu['menu_id'] . "><img src=" . IMAGEPATH . "arrow_down.png width=\"15px\" height=\"15px\" alt=\"agent24\" /></a></td>";
            echo "    <td><a class=\"btn btn-mini btn-warning\" href=" . site_url() . "/admin/menu_controller/edit/" . $menu['menu_id'] . ">Bearbeiten</a>  <a class=\"btn btn-mini btn-danger\" href=" . site_url() . "/admin/menu_controller/delete/" . $menu['menu_id'] . ">L&ouml;schen</a></td>";
            echo "</tr>";
            
            if(!empty($menu['submenu'])) {
                submenusTree($menu['submenu'], ++$depth);
            }
        }

    }

?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $("#language").change(function() {
            //console.log($("#language").val());
            //alert($(location).attr('href'));
            /*
            $.ajax({  
              type: "POST",
              url: "<?php echo site_url(); ?>" + "/router/filter_pages/" + $("#language").val(),
              warning: function() {  
                  
              }
            });
            return false;
            */
           $(location).attr('href',"<?php echo site_url(); ?>" + "/router/view_menus/" + $("#language").val());
        });
    });  
</script>

<h2><?php $translate->__('View menu options'); ?></h2>
<form method="POST">
    <table cellpadding="0" cellspacing="0" width="100%"> 
        <tr>
            <td><?php $translate->__('Language'); ?></td>
            <td>
                <select id="language" name="language">
                    <?php
                        if (!empty($language)) {
                    ?>                    
                            <option value=<?php echo $language['language_id'] ?>><?php echo $language['name'] ?></option>
                    <?php
                        }
                    ?>                    
                    <?php
                        if (!empty($languages)) {
                            foreach ($languages as $key => $language) {
                    ?>
                                <option value=<?php echo $language['language_id'] ?>><?php echo $language['name'] ?></option>
                    <?php
                            }
                        }
                    ?>
                </select>
            </td>
        </tr>
    </table>
</form>
<table class="table table-striped" cellpadding="0" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th><?php $translate->__('Name'); ?></th>
            <th><?php $translate->__('Order'); ?></th>
            <th><?php $translate->__('Options'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
            if (!empty($menus)) {
                foreach ($menus as $key => $menu) {
        ?>
                    <tr>
                        <td><?php echo $menu['name'] ?></td>
                        <td><a href="<?php echo site_url() . "/admin/menu_controller/order/0/" . $menu['menu_id'] ?>"><img src="<?php echo IMAGEPATH . "arrow_up.png" ?>" width="15px" height="15px" alt="agent24" /></a> | <a href="<?php echo site_url() . "/admin/menu_controller/order/1/" . $menu['menu_id'] ?>"><img src="<?php echo IMAGEPATH . "arrow_down.png" ?>" width="15px" height="15px" alt="agent24" /></a></td>
                        <td><a class="btn btn-mini btn-warning" href="<?php echo site_url() . "/admin/menu_controller/edit/" . $menu['menu_id'] ?>"><?php $translate->__('edit'); ?></a>  <a class="btn btn-mini btn-danger" href="<?php echo site_url() . "/admin/menu_controller/delete/" . $menu['menu_id'] ?>"><?php $translate->__('delete'); ?></a></td>
                    </tr>
                    <?php
                        if (!empty($menu['leafs'])) {
                            submenusTree($menu['leafs'], 0);
                        }
                    ?>
        <?php
                }
            }
        ?>
    </tbody>
</table>